package indi.kider.server.applet.service;

import indi.kider.common.constant.AppUserType;
import indi.kider.common.constant.BusStateCons;
import indi.kider.common.constant.StatusCons;
import indi.kider.common.model.apabus.Bus;
import indi.kider.common.model.apabus.Rent;
import indi.kider.common.model.apabus.ServiceDelivery;
import indi.kider.common.model.employee.Waiter;
import indi.kider.server.apabus.mapper.BusMapper;
import indi.kider.server.applet.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.vankid.common.model.base.CommonResponse;

import java.util.Date;
import java.util.List;

@Service
@SuppressWarnings("ALL")
public class BusServiceImpl implements BusService {

    @Autowired
    private BusMapper busMapper;

    @Override
    public CommonResponse<List<Bus>> getBusInfo(Bus bus,User user) {
        try {
            //租客
            if(AppUserType.TENANT == user.getType() || AppUserType.FRIEND == user.getType()){
                bus.setTenantId(user.getId());
                //房東
            }else if(AppUserType.LANDLORD == user.getType()){
                throw new Exception("暂不支持房东登陆");
                //工人
            }else if(AppUserType.WORKER == user.getType()){
                //bus.setWorkdId(user.getId());
                bus.setWaiter(user.getId());
                //服务人员
            }else if(AppUserType.WAITER == user.getType()){
                bus.setWaiter(user.getId());
                //合作企業
            }else if(AppUserType.COOPERATE == user.getType()){
                bus.setEmployeeId(user.getId());
            }else{
                throw new Exception("错误的用户类型！");
            }

            List<Bus> busList = busMapper.select(bus);
            if(busList == null || busList.isEmpty()){
                return  CommonResponse.buildGetSuccessResponse(null);
            }else{
                return CommonResponse.buildGetSuccessResponse(busList);
            }
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @Override
    public CommonResponse<String> updateBusInfo(Bus bus) {
        try {
            busMapper.updateByPrimaryKeySelective(bus);
            return  CommonResponse.buildUpdateSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @Override
    public void rentBusSubmit(Bus bus,User user){
        try {
            //租客
            if(AppUserType.TENANT == user.getType() || AppUserType.FRIEND == user.getType()){
                bus.setTenantId(user.getId());
                bus.setState(1);
                bus.setInsTime(new Date());
                busMapper.insertSelective(bus);
            }
        } catch (Exception e) {
            //return CommonResponse.buildExceptionResponse(e);
        }
    }
}
